﻿using FieldTools.Client.Command;
using FieldTools.Client.Model.Window;
using FieldTools.Util;
using HandyControl.Controls;
using ICSharpCode.AvalonEdit.Document;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Schema;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Json;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace FieldTools.Client.ViewModel.Window
{
    /// <summary>
    /// 
    /// </summary>
    public class CacheViewModel : NotificationObject, ISingletonDependency
    {
        /// <summary>
        /// 
        /// </summary>
        public CacheViewModel()
        {
            Document = new();
            DocumentValue = new();
            ReadCahce.Execute(null);
        }

        #region 实体
        CacheModel obj = new CacheModel();

        private TextDocument document;
        /// <summary>
        /// 
        /// </summary>
        public TextDocument Document
        {
            get { return document; }
            set { document = value; this.RaisePropertyChanged(nameof(Document)); }
        }

        private TextDocument documentValue;
        /// <summary>
        /// 
        /// </summary>
        public TextDocument DocumentValue
        {
            get { return documentValue; }
            set { documentValue = value; this.RaisePropertyChanged(nameof(DocumentValue)); }
        }
        /// <summary>
        /// 
        /// </summary>
        public Dictionary<string, object>? CacheItems
        {
            get { return obj.CacheItems; }
            set { obj.CacheItems = value; this.RaisePropertyChanged(nameof(CacheItems)); }
        }

        #endregion

        #region 事件
        private BaseCommand readCahce;
        /// <summary>
        /// 执行事件，获取缓存
        /// </summary>
        public BaseCommand ReadCahce
        {
            get
            {
                readCahce ??= new BaseCommand(new Action<object>(o =>
                    {
                        CacheItems = CacheHelper.GetFileCacheAll();
                    }));
                return readCahce;
            }
        }

        private BaseCommand execute;
        /// <summary>
        /// 执行事件，显示缓存
        /// </summary>
        public BaseCommand Execute
        {
            get
            {
                execute ??= new BaseCommand(new Action<object>(o =>
                    {
                        ShowCacheItem(o);
                    }));
                return execute;
            }
        }

        private BaseCommand removeCache;
        /// <summary>
        /// 执行事件，移除缓存
        /// </summary>
        public BaseCommand RemoveCache
        {
            get
            {
                removeCache ??= new BaseCommand(new Action<object>(o =>
                {
                    RemoveCacheItem(o);
                }));
                return removeCache;
            }
        }

        private BaseCommand changeCache;
        /// <summary>
        /// 执行事件，修改缓存
        /// </summary>
        public BaseCommand ChangeCache
        {
            get
            {
                changeCache ??= new BaseCommand(new Action<object>(o =>
                {
                    ChangeCacheItem(o);
                }));
                return changeCache;
            }
        }
        #endregion

        #region 方法
        /// <summary>
        /// 显示缓存
        /// </summary>
        /// <param name="o"></param>
        private void ShowCacheItem(object o)
        {
            if (o != null)
            {
                if (o is ListBox listBox)
                {
                    if (listBox.SelectedItem != null)
                    {
                        //var item = listBox.SelectedItem;
                        var item = (KeyValuePair<string, object>)listBox.SelectedItem;
                        Document.Text = item.Value.ToJson().FormatJsonString();
                        var data = (CacheData)item.Value;
                        if (data != null && data.Value != null)
                        {
                            var jsonStr = data.Value.ToString();
                            if (jsonStr != null)
                            {
                                DocumentValue.Text = Regex.Unescape(jsonStr).FormatJsonString();
                            }

                        }
                    }

                }
            }
        }
        /// <summary>
        /// 移除缓存
        /// </summary>
        /// <param name="o"></param>
        private void RemoveCacheItem(object o)
        {
            if (o != null)
            {
                if (o is ListBox listBox)
                {
                    if (listBox.SelectedItem != null)
                    {
                        //var item = listBox.SelectedItem;
                        var item = (KeyValuePair<string, object>)listBox.SelectedItem;
                        CacheHelper.RemoveCache(item.Key);
                        Document.Text = string.Empty;
                        DocumentValue.Text = string.Empty;
                        CacheItems = CacheHelper.GetFileCacheAll();
                        MessageBox.Success("删除成功");
                    }

                }
            }
        }

        /// <summary>
        /// 修改缓存条目内容
        /// </summary>
        /// <param name="o"></param>
        private void ChangeCacheItem(object o)
        {
            if (o != null)
            {
                if (o is ListBox listBox)
                {
                    if (listBox.SelectedItem != null)
                    {
                        //var item = listBox.SelectedItem;
                        var item = (KeyValuePair<string, object>)listBox.SelectedItem;
                        var jsonStr = DocumentValue.Text;
                        if (!string.IsNullOrEmpty(jsonStr))
                        {
                            try
                            {
                                _ = jsonStr.ToObject<object>();
                                CacheHelper.SetCache(item.Key, jsonStr);
                                CacheItems = CacheHelper.GetFileCacheAll();
                                ShowCacheItem(o);
                                MessageBox.Success("修改成功");
                            }
                            catch (Exception)
                            {
                                MessageBox.Warning("json不合法，请检查");
                            }
                        }
                    }

                }
            }
        }
        #endregion
    }
}
